<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="keyName" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getEmp">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="emps" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="30">
			</el-table-column>
			<el-table-column prop="id" label="员工编号" width="150" sortable>
			</el-table-column>
			<el-table-column prop="username" label="员工名称" width="150"  sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="150"  sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话" width="150" sortable>
			</el-table-column>
			<!--<el-table-column prop="salt" label="盐" min-width="150" sortable>
			</el-table-column>-->
      <el-table-column prop="password" label="密码" min-width="150" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" min-width="150" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="150"  sortable>
        <template scope="scope">
          <span v-if="scope.row.state=='0'" style="color: green">启用</span>
          <span v-if="scope.row.state=='1'" style="color: red">禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="department.name" label="部门" min-width="150" sortable>
      </el-table-column>
      <el-table-column prop="logininfo.username" label="用户名" min-width="150" sortable>
      </el-table-column>
      <el-table-column prop="shop.name" label="公司" min-width="150" sortable>
      </el-table-column>
			<el-table-column label="操作" min-width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" :current-page="currentPage"  style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="姓名" prop="name">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="email" prop="name">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
        </el-form-item>
          <el-form-item label="电话" prop="name">
            <el-input v-model="editForm.phone" auto-complete="off"></el-input>
          </el-form-item>
            <el-form-item label="密码" prop="name">
              <el-input v-model="editForm.password" auto-complete="off"></el-input>
            </el-form-item>
              <el-form-item label="年龄" prop="name">
                <el-input v-model="editForm.age" auto-complete="off"></el-input>
              </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="editForm.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="部门">
          <el-select v-model="editForm.department" value-key="id" clearable placeholder="请选择">
            <el-option v-for="item in departments" :key="item.id" :label="item.name" :value="item">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="公司">
          <el-select v-model="editForm.shop" value-key="id" clearable placeholder="请选择">
            <el-option v-for="item in shops" :key="item.id" :label="item.name" :value="item">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.tel }}</span>
            </el-option>
          </el-select>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<!--<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="姓名" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="性别">
					<el-radio-group v-model="addForm.sex">
						<el-radio class="radio" :label="1">男</el-radio>
						<el-radio class="radio" :label="0">女</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="年龄">
					<el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
				</el-form-item>
				<el-form-item label="生日">
					<el-date-picker type="date" placeholder="选择日期" v-model="addForm.birth"></el-date-picker>
				</el-form-item>
				<el-form-item label="地址">
					<el-input type="textarea" v-model="addForm.addr"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>-->
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				emps: [],
        title:'',
        keyName:'',
        departments:[],
        logininfos:[],
        shops:[],
				total: 0,
        currentPage: 1,
        pageSize: 10,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
					/*	{ required: true, message: '请输入姓名', trigger: 'blur' }*/
					]
				},
				//编辑界面数据
				editForm: {
					id: null,
					username: '',
          email:'',
          phone:'',
          password:'',
          age:0,
          state: 1,
          department:null,
          logininfo:null,
          shop:null
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						/*{ required: true, message: '请输入姓名', trigger: 'blur' }*/
					]
				},
		/*		//编辑界面数据
				addForm: {
          id: null,
          username: '',
          email:'',
          phone:'',
          password:'',
          age:0,
          state: 1,
          department:null,
          logininfo:null,
          shop:null
				}*/

			}
		},
		methods: {
			//性别显示转换
			/*formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},*/
			handleCurrentChange(val) {
				this.currentPage = val; //修改currentPage
				this.getEmp();
			},
			//获取员工列表
      getEmp() {
				let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyName:this.keyName
				};
				this.listLoading = true; //忙等
        //部门列表页
				this.$http.post("/emp",para).then(result=>{
          this.listLoading = false; //关闭忙等
				  this.emps = result.data.pageList
          this.total = result.data.total
        }).catch(result=>{
          this.listLoading = false; //关闭忙等
        })
			},
      getShop(){
        this.$http.get("/shop").then(result=>{
          this.employees = result.data
        }).then(result=>{
        })
      },
      getlogin(){
        this.$http.get("/login").then(result=>{
          this.employees = result.data
        }).then(result=>{
        })
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true; //开启忙等
					this.$http.delete("/emp/"+row.id).then(result=>{
					  if(result.data.state){
					    this.message({
                message:result.data.message,
                type:'success'
					    });
            }else{
					    this.$message({
                $message:result.data.message,
                type:'error'
              });
            }
          })
          this.listLoading = false; //关闭忙等
          this.currentPage=1
          this.getEmp()
				}).catch(result=>{
          this.listLoading = false; //关闭忙等
        })
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.getShop()
        this.getlogin()
        this.title = "编辑"
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.getShop()
        this.getlogin()
        this.title = "新增"
        this.editFormVisible = true;
				this.editForm= {
          id: null,
          username: '',
          email:'',
          phone:'',
          password:'',
          age:0,
          state: 1,
          department:null,
          logininfo:null,
          shop:null
        }
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.editForm);
              /*if(para.parent){
                //将数组转为对象
                para.parent={"parent_id":para.parent[para.parent.length-1]}
              }*/

             /* para.parent_id=para.parent_id[para.parent_id.length-1]
              para.manager_id=para.manager_id*/
              console.log(para);
              this.$http.put("/emp",para).then(result=>{
                //manager: null,
                //parent:null,
                //  para.parent_id =
                //manager_id:null
                if(result.data.state){
                  this.$message({
                    message:result.data.message,
                    type:"success"
                  });
                  //成功关闭模态框
                  this.editFormVisible = false
                } else{
                  this.$message({
                    message:result.data.message,
                    type:"error"
                  });
                }
                this.editLoading=false;
                this.getDept()
              }).catch(result=>{
                this.editLoading=false;
              })
            });
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					//this.listLoading = true;
					//NProgress.start();
          console.log(ids)
          this.$http.patch("/emp",ids).then(result=>{
            if(result.data.state){
              this.$message({
                message:result.data.message,
                type:'success'
              })
              this.currentPage=1
              this.getEmp() //获取用户数据
            }else {
              this.$message({
                message:result.data.message,
                type:'error'
              })
            }
          })
					/*let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEmp();
					});*/
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getEmp();
		}
	}

</script>

<style scoped>

</style>